package com.wx.mall.order.controller;


import com.alibaba.fastjson.JSON;

import com.wx.mall.common.api.CommonPage;
import com.wx.mall.common.api.CommonResult;
import com.wx.mall.common.api.ResultCode;
import com.wx.mall.common.exceptions.ApiAssert;

import com.wx.mall.common.vo.order.OrderRepVo;
import com.wx.mall.order.dto.OrderSelectDto;
import com.wx.mall.order.entity.Order;
import com.wx.mall.order.service.IOrderService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author j
 * @since 2020-04-08
 */
@Api(tags = "订单列表",description = "OrderController")
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {


   @Resource
    private IOrderService iOrderService;




    @GetMapping("/selectById")
    @ApiOperation("根据id查询订单")
    public CommonResult<Order> selectById(Integer id) {

       Order list = iOrderService.getById(id);
        return CommonResult.success(list);
    };


    @GetMapping("/selectByStatus")
    @ApiOperation("查询未发货的订单列表")
    public CommonResult<List<Order>> selectByStatus() {

        List<Order> list = iOrderService.selectByStatus();
        return CommonResult.success(list);
    };

    @GetMapping("/page")
    @ApiOperation("分页查询订单列表")
    public CommonResult<CommonPage<Order>> pageInfo(Integer pageNum,
                                                   Integer pageSize) {
        if (pageNum == null || pageSize == null) {
            ApiAssert.fail(ResultCode.VALIDATE_FAILED);
        }
        if (pageNum <= 0) {
            ApiAssert.fail("无效页码");
        }
        List<Order> list = iOrderService.find(pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(list));
    };


    @GetMapping("/pageOrder")
    @ApiOperation("分页查询订单列表并排序")
    public CommonResult<CommonPage<Order>> pageOrder( Integer pageNum,
                                                     Integer pageSize,int sort) {
        if (pageNum == null || pageSize == null) {
            ApiAssert.fail(ResultCode.VALIDATE_FAILED);
        }
        if (pageNum <= 0) {
            ApiAssert.fail("无效页码");
        };
        //     sort:   0为创建时间 1为支付方式 2订单来源  3订单状态
        List<Order> list = iOrderService.pageOrder(pageNum,pageSize,sort);
        return CommonResult.success(CommonPage.restPage(list));
    };

    @GetMapping("/pageOrderTwo")
    @ApiOperation("分页查询已经发货的订单列表并排序")
    public CommonResult<CommonPage<Order>> pageOrderTwo( Integer pageNum,
                                                      Integer pageSize,int sort) {
        if (pageNum == null || pageSize == null) {
            ApiAssert.fail(ResultCode.VALIDATE_FAILED);
        }
        if (pageNum <= 0) {
            ApiAssert.fail("无效页码");
        };
        //     sort:   0为创建时间 1为支付方式 2订单来源  3订单状态
        List<Order> list = iOrderService.pageOrderTwo(pageNum,pageSize,sort);
        return CommonResult.success(CommonPage.restPage(list));
    };


    @GetMapping("/pageSelect")
    @ApiOperation("分页查询订单列表并筛选还排序")
    public CommonResult<CommonPage<Order>> pageSelect(OrderSelectDto dto) {
        if (dto.getPageNum() == null || dto.getPageSize() == null) {
            ApiAssert.fail(ResultCode.VALIDATE_FAILED);
        }
        if (dto.getPageNum() <= 0) {
            ApiAssert.fail("无效页码");
        }
        //     sort:   0为创建时间 1为支付方式 2订单来源  3订单状态
        List<Order> list = iOrderService.pageSelect(dto.getPageNum(),dto.getPageNum(),dto.getSort(),dto.getOrderStatus(),dto.getTransactionId(),dto.getReceiverMobile(),dto.getCreateTime(),dto.getSourceType());
        return CommonResult.success(CommonPage.restPage(list));
    };

    @GetMapping("/pageSelectTwo")
    @ApiOperation("分页查询订单列表并筛选还排序(查询的是已经发货的列表)")
    public CommonResult<CommonPage<Order>> pageSelectTwo(OrderSelectDto dto) {
        if (dto.getPageNum() == null || dto.getPageSize() == null) {
            ApiAssert.fail(ResultCode.VALIDATE_FAILED);
        }
        if (dto.getPageNum() <= 0) {
            ApiAssert.fail("无效页码");
        }
        //     sort:   0为创建时间 1为支付方式 2订单来源  3订单状态
        List<Order> list = iOrderService.pageSelectTwo(dto.getPageNum(),dto.getPageNum(),dto.getSort(),dto.getTransactionId(),dto.getReceiverMobile(),dto.getCreateTime(),dto.getSourceType());
        return CommonResult.success(CommonPage.restPage(list));
    };

    @ApiOperation("批量关闭订单")
    @PutMapping("/updateListClose")
    public CommonResult<Order> updateListClose(OrderRepVo vo){

        int suceess=iOrderService.updateListClose(vo);

        return suceess==1?CommonResult.success():CommonResult.failed();


    };

    @ApiOperation("批量修改订单为已完成")
    @PutMapping("/updateListSuccess")
    public CommonResult<Order> updateListSuccess(OrderRepVo vo){

        int suceess=iOrderService.updateListSuccess(vo);

        return suceess==1?CommonResult.success():CommonResult.failed();


    };

    @ApiOperation("批量删除订单软三")
    @DeleteMapping("/deleteListClose")
    public CommonResult<Order> deleteList(OrderRepVo vo){

        int suceess=iOrderService.deleteList(vo);

        return suceess==1?CommonResult.success():CommonResult.failed();


    };


    @ApiOperation("单个关闭或删除订单软三(修改订单)")
    @PutMapping("/updateOne")
    public CommonResult<Integer> updateOne(Order order){
        System.out.println(order);

        boolean suceess=iOrderService.updateById(order);

        return suceess?CommonResult.success():CommonResult.failed();


    };

    @ApiOperation("批量修改订单并发货")
    @PutMapping("/updateAll")
    public CommonResult<Order> updateAll(List<Order> orderList){
        System.out.println(orderList);
//        JSONObject jsonObject = JSONObject.parseObject(orderList);
//        List<Order> share = JSON.toJavaObject(jsonObject,Order.class );
        List<Order> datas = JSON.parseArray(JSON.toJSONString(orderList),Order.class);
        System.out.println(datas);
        int suceess=1;
//                iOrderService.updateOne(vo);

        return suceess==1?CommonResult.success():CommonResult.failed();


    };




    @ApiOperation("查询记录")
    @GetMapping("/getAllObjs")
    public CommonResult<Integer> getAllObjs(Integer orderStatus){

      int ss=iOrderService.getAllObjs(orderStatus);

        return CommonResult.success(ss);


    };




}

